package org.vectrics.common.util;

import java.io.FileOutputStream;

import org.apache.log4j.*;
//import org.apache.xml.serialize.OutputFormat;
//import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.*;

public class XmlUtil {
    private static Category log = Category.getInstance(XmlUtil.class);

    public static String getChildElementAttribute(Element element, String name) {
        log.debug("getChildElementAttribute() called.");
        String value = null;
        
        // Process Element children that are properties.
        NodeList nodeChildren = element.getChildNodes();
        int i = 0;
        boolean done = false;
        while ((i < nodeChildren.getLength()) && (done == false)) {
            Node node = nodeChildren.item(i);
            
            //log.debug("getChildElementAttribute() - checking node to see if it is element and matches name passed.");

            if ((node.getNodeType() == Node.ELEMENT_NODE) && (node.getNodeName() != null)) {
                if (node.getNodeName().equals(name)) {
                    Element childElement = (Element)nodeChildren.item(i);
                    done = true;
                    value = getElementContent(childElement);
                    
                }
            }
            
            i++;
        }

        if (log.isDebugEnabled()) {
            log.debug("getChildElementAttribute() - done - Name = " + name 
                    + "   Value = " + value);
        }
        return(value);
    }
 


    public static String getElementContent(Element childElement) {
        String value = null;
        NodeList elementChildren = childElement.getChildNodes();
        for(int n = 0; n < elementChildren.getLength(); n ++)
         {  
            Node node = elementChildren.item(n);
            if (elementChildren.item(n).getNodeType() == Node.TEXT_NODE) {
            //children.item(n).getNodeType() == Node.CDATA_SECTION_NODE )
                if (value == null) {
                    value = "";
                }
                
               value += elementChildren.item(n).getNodeValue();
            }
            //else {
            //    log.info("getElementContent() - Name = " + node.getNodeName() + "   Value = " + node.getNodeValue());
            //}
         }
         log.debug("getElementContent() - value = " + value);
         return(value);
    }
/*
	public static void writeXmlDocumentToFile(Document document, String filePath) {
		// Serialize the document
		OutputFormat format = new OutputFormat(document);
		format.setLineWidth(80);
		format.setIndenting(true);
		format.setIndent(2);
		try {
			FileOutputStream outFile = new FileOutputStream(filePath, false);
			XMLSerializer serializer = new XMLSerializer(outFile, format);
			serializer.serialize(document);
			outFile.close();
		} catch (Exception ex) {
			throw new RuntimeException("Exception writing XML to file: " + filePath);
		}
	}
*/
}

